# 客户端对象
class RedisClient:
    """
    redis客户端类，\n
    clientIP\n
    clientPort\n
    redisDbId: 当前使用的数据库id\n
    保存客户的信息\n
    """

    def __init__(self, conn, clientIP, clientPort, redisDbId=0, redis_multi=0):
        self.conn = conn
        self.clientIP = clientIP
        self.clientPort = clientPort
        self.redisDbId = redisDbId  # 设置默认使用第0个数据库
        self.redis_multi = redis_multi  # 默认不开启事务


# 服务端对象
class RedisServer:
    """
    redis服务端类\n
    """

    def __init__(self, redisDbNum=16):
        self.rDB = {i: RedisDatabase() for i in range(redisDbNum)}  # 设置默认的数据库个数为16个
        self.redisDbNum = redisDbNum


# redis 数据库
class RedisDatabase:
    """
    redis 数据库类\n
    """

    def __init__(self):
        self.sds_data = {}
        self.list_data = {}
        self.set_data = {}
        self.zset_data = {}
        self.hash_data = {}
        self.pubsub_channels = {}

    # 获取数据库实例的键-类型
    def get_keys(self):
        """
        获取数据库中的所有的键与类型\n
        :return: {'key1': 'type1','key2': 'type2', ....}
        """
        keys_dict = {}
        for key in self.sds_data.keys():
            keys_dict[key] = 'string'

        for key in self.list_data.keys():
            keys_dict[key] = 'list'

        for key in self.set_data.keys():
            keys_dict[key] = 'set'

        for key in self.zset_data.keys():
            keys_dict[key] = 'sortedset'

        for key in self.hash_data.keys():
            keys_dict[key] = 'hash'

        for key in self.pubsub_channels.keys():
            keys_dict[key] = 'pubsub_channels'

        return keys_dict

    # 删除字典所有的键值对
    def set_keys_null(self):
        """
        删除一个数据库的所有的键\n
        :return:
        """
        try:
            self.sds_data.clear()
            self.list_data.clear()
            self.set_data.clear()
            self.zset_data.clear()
            self.hash_data.clear()
            self.pubsub_channels.clear()

            return True
        except Exception as e:
            return False

    # 删除指定的一个或多个键
    def del_key_list(self, key_list):
        add = 0
        for key in key_list:
            if self.sds_data.pop(key, None):
                add += 1
            if self.list_data.pop(key, None):
                add += 1
            if self.set_data.pop(key, None):
                add += 1
            if self.zset_data.pop(key, None):
                add += 1
            if self.hash_data.pop(key, None):
                add += 1
            if self.pubsub_channels.pop(key, None):
                add += 1
        return add
